package com.wiegand {
	import com.pixelbreaker.ui.osx.MacMouseWheel;
	import com.wiegand.error.ErrorHandler;
	import com.wiegand.event.LoaderEvent;
	import com.wiegand.facebook.Facebook;
	import com.wiegand.loader.CssLoader;
	import com.wiegand.loader.FontLoader;
	import com.wiegand.loader.SoundLoader;
	import com.wiegand.loader.SwfLoader;
	import com.wiegand.loader.XmlLoader;
	import com.wiegand.loader.loaderdisplay.MainLoader;
	import com.wiegand.util.CustomizedContextMenu;
	import com.wiegand.util.ExtendedArray;

	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;

	/**
	 * @author kwiegand
	 */
	 
	public class AbstractMain extends Sprite {

		//---DEFAULT VALUES----------------------------------
		//---------------------------------------------------
		private var _xmlpath : String = "../xml/init.xml"; 
		private var _errorPath : String = "../xml/error.xml"; 
		//---------------------------------------------------
		//---------------------------------------------------
	
		protected var _initXml : XML;	
		protected var _contentLayer: Sprite;
		private var _errorLayer: Sprite;
		private var _loaderLayer: Sprite;
		protected var _objectsToResize : Array;
							
		public function AbstractMain(){
			
			Facebook.getInstance().checkJS();
			
			stage.stageFocusRect = false;
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.quality = StageQuality.HIGH;
			stage.displayState = StageDisplayState.NORMAL;
			stage.addEventListener(Event.RESIZE, resizeHandler);
			
			MacMouseWheel.setup(stage);
			
			new CustomizedContextMenu(this);
			
			init();			
		}
		
		private function init():void{					
			var p : String = root.loaderInfo.parameters["xmlpath"] as String;
			if( p != null ) _xmlpath = p;	
			
			var dict:ExtendedArray = new ExtendedArray();
			Configuration.getInstance().dict = dict;
			
			_contentLayer = new Sprite();
			_contentLayer.mouseEnabled = false;
			addChild(_contentLayer);
			
			_loaderLayer = new Sprite();
			_loaderLayer.mouseEnabled = false;
			addChild(_loaderLayer);
			
			_errorLayer = new Sprite();
			_errorLayer.mouseEnabled = false;
			_errorLayer.mouseChildren = false;
			addChild(_errorLayer);
			
//			var stats : Stats = new Stats( { bg: 0x202020, fps: 0xffffff, ms: 0xffffff, mem: 0xffffff, memmax: 0xffffff } );
//			stats.alpha = .6;
//			addChild( stats );
			
			_errorLayer.addChild( ErrorHandler.getInstance() );
			Configuration.getInstance().dict.add(ErrorHandler.getInstance(), "ErrorHandler");

			showPreLoader();
		}
		
		private function showPreLoader():void{
			this.addEventListener(Event.ENTER_FRAME, stageListener);
		}
				
		private function stageListener(e:Event):void{	
			if(stage.stageWidth>0){
				this.removeEventListener(Event.ENTER_FRAME, stageListener);
				ErrorHandler.getInstance().initStageSize(stage.stageWidth, stage.stageHeight);
				_loaderLayer.addChild( MainLoader.getInstance() );
				MainLoader.getInstance().initialize();
				MainLoader.getInstance().open();		
				Configuration.getInstance().dict.add(MainLoader.getInstance(), "MainLoader");	
				loadErrorXML();
			}
		}
		
		private function loadErrorXML( e : LoaderEvent = null ):void{
			trace("start loading...");
			trace("-----------------------");
			XmlLoader.getInstance().load( [ _errorPath ] );
			XmlLoader.getInstance().addEventListener( LoaderEvent.EVENT_LOADING_COMPLETE, loadInitXML );	
			MainLoader.getInstance().update(10);
		}
		
		private function loadInitXML( e : LoaderEvent = null ):void{
			XmlLoader.getInstance().removeEventListener( LoaderEvent.EVENT_LOADING_COMPLETE, loadInitXML );
			var errorXML: XML = ( XmlLoader.getInstance().getXml(0) as XML );
			ErrorHandler.getInstance().initErrorXML(errorXML);
			trace("error xml loading complete");
			trace("load: "+_xmlpath);
			XmlLoader.getInstance().clear();
			XmlLoader.getInstance().load( [ _xmlpath ] );
			XmlLoader.getInstance().addEventListener( LoaderEvent.EVENT_LOADING_COMPLETE, loadSWF );
			MainLoader.getInstance().update(20);
		}
		
		private function loadSWF( e : LoaderEvent = null ):void{
			trace("init xml loading complete");
			XmlLoader.getInstance().removeEventListener( LoaderEvent.EVENT_LOADING_COMPLETE, loadSWF );
			_initXml = ( XmlLoader.getInstance().getXml(0) as XML );
			var swfFile : String = String( _initXml[ "swfSource" ] );	
			SwfLoader.getInstance().load( swfFile );			
			SwfLoader.getInstance().addEventListener( LoaderEvent.EVENT_LOADING_COMPLETE, loadCSS );
			MainLoader.getInstance().update(40);
		}
		
		private function loadCSS( e : LoaderEvent = null ):void{
			trace("swf loading complete");
			SwfLoader.getInstance().removeEventListener( LoaderEvent.EVENT_LOADING_COMPLETE, loadCSS );
			var cssFile : String = String( _initXml[ "cssSource" ] );	
			CssLoader.getInstance().load( cssFile );
			CssLoader.getInstance().addEventListener( LoaderEvent.EVENT_LOADING_COMPLETE, loadFonts );
			MainLoader.getInstance().update(60);
		}
		
		private function loadFonts( e : LoaderEvent = null ):void{
			trace("css loading complete");
			CssLoader.getInstance().removeEventListener( LoaderEvent.EVENT_LOADING_COMPLETE, loadFonts );
			var fontArray : String = String( _initXml[ "fonts" ] );	
			var fontPath : String = String( _initXml[ "fontSource" ] );	
			var fontPackage : String = String( _initXml[ "fontPackage" ] );	
			FontLoader.getInstance().load( fontPackage, fontArray, fontPath );
			FontLoader.getInstance().addEventListener( LoaderEvent.EVENT_LOADING_COMPLETE, loadSounds );
			MainLoader.getInstance().update(70);
		}
		
		private function loadSounds( e : LoaderEvent = null ):void{
			trace("fonts loading complete");
			CssLoader.getInstance().removeEventListener( LoaderEvent.EVENT_LOADING_COMPLETE, loadFonts );
			SoundLoader.getInstance().load( _initXml[ "soundSources" ] );
			SoundLoader.getInstance().addEventListener( LoaderEvent.EVENT_LOADING_COMPLETE, loadingComplete );
			MainLoader.getInstance().update(80);
		}
		
		private function loadingComplete( e : LoaderEvent = null ):void{
			trace("sounds loading complete");
			SoundLoader.getInstance().removeEventListener( LoaderEvent.EVENT_LOADING_COMPLETE, loadingComplete );
			trace("-----------------------");
			trace("...loading complete\n");	
			trace("++++++++++++++++++++++++++++++++++++");	
			trace("++++++++++++++++++++++++++++++++++++");	
			trace("\n");	
			trace("\n");	
			MainLoader.getInstance().addEventListener(MainLoader.EVENT_LOADER_FINISHED, loaderFinished);			
			MainLoader.getInstance().addEventListener(MainLoader.EVENT_LOADER_CLOSED, loaderClosed);			
			MainLoader.getInstance().update(100);			
		}
		
		private function loaderFinished(e:Event) : void {	
			MainLoader.getInstance().removeEventListener(MainLoader.EVENT_LOADER_FINISHED, loaderFinished);
			startApplication();		
		}
		
		private function loaderClosed(e:Event) : void {	
			MainLoader.getInstance().removeEventListener(MainLoader.EVENT_LOADER_CLOSED, loaderClosed);
			Configuration.getInstance().dict.removeByName("MainLoader");
			MainLoader.getInstance().clear();
			_loaderLayer.removeChild(MainLoader.getInstance());		
		}
		
		protected function startApplication() : void {	
			//to override		
		}
		
		protected function resizeHandler(e:Event) : void {
			Configuration.getInstance().dict.toString();
			for(var i:int=0;i<Configuration.getInstance().dict.length;i++){
				Configuration.getInstance().dict.getObjectByIndex(i).resize();
			}
		}
		protected function registerToResizeHandler( obj : Object ) : void {
			_objectsToResize.push(obj);
		}
		
	}
}

